// pages/FunctionList/FunctionList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    open_identify:false,
    open_text2word:false,
    showModal: false, // 显示modal弹窗
    single: false, // false 只显示一个按钮，如果想显示两个改为true即可
    ocr_content:"",
    modal_show:false, //模态框是否显示 默认false 不显示
    show_filename:false, //导出为word弹出的保存文件的弹出框
    filename:"",
    selectbtn_show:true,   //箭头切换
    isExsitFile:false,
    selectcontent:[],
    value:undefined,   //选中的值
    valueid:undefined,  //选中的id
    type:"new",  //判断是哪种操作 新建文件 还是添加到已有文件 默认是新建文件
    filename:"",
    show_append_text:false //添加文字modal框
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this 
    let type = options.type
    if(type!=undefined){
      that.setData({
            type:type
      })
    }
    let filename = options.filename
    if(filename != undefined){
      that.setData({
            filename:filename
      })
    }
  },
  copyText(e){
    let that = this
    let data = that.data.ocr_content
    wx.setClipboardData({
      data: that.data.ocr_content,
      success (res) {
        // that.setData({
        //   modal_show:false
        // })
      }
    })
  },
    //导出为word
    exportTxt(e){
      let that = this
      let type = that.data.type
      if(type!="append"){
        that.setData({
          show_filename:true
        })
      }else{
        //这个是首页过来的追加文字请求
        let filename = that.data.filename
        filename = filename.slice(0,filename.indexOf(".docx"))
      
        let ocr_content = that.data.ocr_content
        let openid = wx.getStorageSync('openid')
  
        wx.request({
          method:"post",
          header: {
            'content-type': 'application/json' 
          },
          //url: 'http://121.37.180.71:8002/sendAppendContent', 
          url:'https://admin.yzqysoft.com:8002/sendAppendContent',
          data: {
            fileName:filename,
            content:ocr_content,
            openid:openid
          },    
          success (res) {          
            // 预览网络文档
            wx.downloadFile({
              //url: "http://121.37.180.71:8002/static/tmp_docx/"+openid+"/"+filename+".docx", // 文件的本身url
              url:"https://admin.yzqysoft.com:8002/static/tmp_docx/"+openid+"/"+filename+".docx", // 文件的本身url
              filePath: wx.env.USER_DATA_PATH + '/'+filename+".docx", // 本地自定义的文件名
              success: function (res) {
                let filePath = res.filePath; // 微信临时文件路径(这里要使用自定义的名字文件名,否则打开的文件名是乱码)
                wx.openDocument({
                  filePath: filePath,
                  showMenu: true,  // 是否显示右上角菜单按钮 默认为false(看自身需求，可要可不要。后期涉及到右上角分享功能)
                  success: function () {
                   wx.showToast({
                     title: '打开文件成功',
                   })
                  },
                  fail: function() {
                    wx.showToast({ title: '打开文件失败，请稍后重试' });
                  }
                });           
              },
              fail: function() {
                wx.showToast({ title: '打开文件失败，请稍后重试' });           
              }
            });
          }
        })
      }  
    },

  /*
  **input_blur 面对有些手机没有完成按钮 使用这个方法获取文件名
  */
  bind_input(e){
    let that = this
    //let openid = wx.getStorageSync('openid')
    /**，event.detail = { value, encryptedValue, encryptError }	 */
    that.setData({
      filename:e.detail.value,
      isExsitFile:false
    })  
  },

  /*新建文件或者选择文件*/
  filename_cancel(){
    let that = this
    that.setData({
      show_filename:false
    })
  },

    /**创建新文件*/
    createNewWord(s){
      let that = this
      let openid = wx.getStorageSync('openid')
      let filename = that.data.filename
      let ocr_content = that.data.ocr_content
      let url = s
      if(filename == "" || filename == undefined){
            wx.showToast({
            title: '请重新输入文件名',
            })      
            return
      }

      wx.request({
        method:"post",
        header: {
          'content-type': 'application/json' 
        },     
        url:url,
        data: {
          fileName:filename,
          content:ocr_content,
          openid:wx.getStorageSync('openid')
        },    
        success (res) {
          // 预览网络文档
          wx.downloadFile({    
            url:"https://admin.yzqysoft.com:8002/static/tmp_docx/"+openid+"/"+filename+".docx", // 文件的本身url
            filePath: wx.env.USER_DATA_PATH + '/'+filename+".docx", // 本地自定义的文件名
            success: function (res) {
              let filePath = res.filePath; // 微信临时文件路径(这里要使用自定义的名字文件名,否则打开的文件名是乱码)
              wx.openDocument({
                filePath: filePath,
                showMenu: true,  // 是否显示右上角菜单按钮 默认为false(看自身需求，可要可不要。后期涉及到右上角分享功能)
                success: function () {
                  wx.showToast({
                    title: '打开文件成功',
                  })            
                  that.setData({
                    show_filename:false,
                    filename:""
                  })
                },
                fail: function() {
                  wx.showToast({ title: '打开文件失败，请稍后重试' });
                }
              });           
            },
            fail: function() {
              wx.showToast({ title: '打开文件失败，请稍后重试' });           
            }
          });
        }
      })
    },

  /**确定选择哪个文件 */
  filename_confirm(){
    let  that = this 
    let filename = that.data.filename
    let ocr_content = that.data.ocr_content
    let isExsitFile = that.data.isExsitFile    

    let url = ""
    let openid = wx.getStorageSync('openid')
    wx.request({
      method:"POST",
      header: {
        'content-type': 'application/json' 
      },  
      url:'https://admin.yzqysoft.com:8002/getExsitFiles',
      data:{
        openid:wx.getStorageSync('openid')
      },
      success:function(res){
         //["doc1", "doc1.docx"]
        let data_arr = res.data      
      
        if(data_arr.length != 0){          
          let exist = false         
       
          /**此处设置一个exsit中是因为 forEach 怎么都不呢能break中断 目前只能这样标记 当前文件名存不存在 */
          data_arr.forEach(function(value,index){            
              let suffix_index = value.indexOf(".docx")
              let data = ""
              if(suffix_index != -1){
                data = value.slice(0, suffix_index)
              }
           
              //||filename.indexOf("已存在,请点击首页追加内容按钮")!=0
              if(data == filename){
                exist = true                           
              }    
          }) 
         
          if(exist){
            that.setData({
              isExsitFile:true, /*选择了已存在的文件*/      
              //filename:filename+"已存在,请点击首页追加内容按钮"
            })            
          }else{
            that.setData({
              filename:filename,
              isExsitFile:false, /**表示是新建的文件*/
            })

            url = 'https://admin.yzqysoft.com:8002/sendNewContent'
            that.createNewWord(url)
          }       
        }else if(data_arr.length == 0){
          that.setData({
            filename:filename,
            isExsitFile:false, /**表示是新建的文件*/
          })

          url = 'https://admin.yzqysoft.com:8002/sendNewContent'
          that.createNewWord(url)
        }  
      },
      fail:function(err){
        console.log(err)
      }
    })   
  },  

  
  //关闭 识别文字框
  close_modal(){  
    this.setData({ modal_show: false }) 
  },

  //关闭文件名框
  close_filename(){
    let that = this
    that.setData({
      show_filename:false
    })
  },

  do_identify(){
    let that = this
    
    let status = that.data.open_identify

    that.setData({
      open_identify:!status
    })

  },

  do_text2word(){
    let that = this
    
    let status = that.data.open_text2word

    that.setData({
      open_text2word:!status
    })
  },

  /**身份证正面 */
  idCardFrontsuccess(info){
    let that = this 

    // if(info.base_resp.err_code == 0 || info.base_resp.err_msg == "success"){
      let result = info.detail
      let address = result.address.text
      let birth = result.birth.text
      let gender = result.gender.text
      let id = result.id.text
      let name = result.name.text
      let nationality = result.nationality.text    

      let content = address+"\n"+birth+"\n"+gender+"\n"+id+"\n"+name+"\n"+nationality

      //新建识别任务
      if(that.data.type != "append"){
        that.setData({
          ocr_content:content,
          modal_show:true
        })
      }else{ //追加文字
        that.setData({
          ocr_content:content,
          show_append_text:true
        })
      }   
  },

  /** 身份证反面*/
  idCardBacksuccess(info){
    let  that = this
    // if(info.base_resp.err_code == 0 || info.base_resp.err_msg == "success"){
      let result = info.detail
      let authority = result.authority.text
      let valid_date = result.valid_date.text  
      
      let content = authority +"\n"+valid_date

      //新建识别任务
      if(that.data.type != "append"){
        that.setData({
          ocr_content:content,
          modal_show:true
        })
      }else{ //追加文字
        that.setData({
          ocr_content:content,
          show_append_text:true
        })
      }  
  },

  /**银行卡识别 */
  bankSuccess(info){
    let that  =  this
    let result = info.detail
    let number = result.number.text

    //新建识别任务
    if(that.data.type != "append"){
      that.setData({
        ocr_content:number,
        modal_show:true
      })   
    }else{
      that.setData({
        ocr_content:number,
        show_append_text:true
      })
    }
  },

  /** 驾驶证识别*/
  driverLicenseSuccess(info){
    let that = this

    let result = info.detail
    let address = "住址: "+result.address.text
    let birth_data = "出生日期: "+result.birth_date.text
    let car_class = "准驾车型: "+result.car_class.text
    let id_num = "证号: "+result.id_num.text
    let issue_date = "初次领证日期: "+result.issue_date.text
    let name = "姓名: " + result.name.text
    let nationality = "国籍: " + result.nationality.text
    let official_seal = "发证机关: "+ result.official_seal.text
    let sex = "性别: "+result.sex.text
    let limit_time = "有效期限: "+result.valid_from.text +" 至 "+ result.valid_to.text

    let content = name+"\n"+sex+"\n"+nationality +"\n"+address +"\n"+birth_data +"\n"+car_class+"\n"+id_num +"\n"+issue_date +"\n"+limit_time +"\n"+official_seal

    //新建识别任务
    if(that.data.type != "append"){
      that.setData({     
        ocr_content:content,
        modal_show:true
      })
    }else{
      that.setData({
        ocr_content:content,
        show_append_text:true
      })
    }


  },

  /**行驶证识别*/
  driverSuccess(info){
    let that = this
    let result = info.detail
    
    let engine_num = "发动机号码: "+result.engine_num.text
    let issue_date = "发证日期: "+result.issue_date.text
    let addr = "住址: "+ result.addr.text
    let model = "品牌型号: "+result.model.text
    let official_seal = "发证机关:" + result.official_seal.text
    let owner = "所有人: " + result.owner.text
    let plate_num =  "号牌号码: "+ result.plate_num.text
    let register_date = "注册日期: "+ result.register_date.text
    let use_character = "使用性质: "+result.use_character.text
    let vehicle_type = "车辆类型: "+result.vehicle_type.text
    let vin = "车辆识别代号: "+ result.vin.text

    let content = plate_num+"\n"+vehicle_type+"\n"+owner +"\n"+addr +"\n"+use_character +"\n"+model+"\n"+vin +"\n"+engine_num +"\n"+register_date +"\n"+issue_date+"\n"+official_seal

    //新建识别任务
    if(that.data.type != "append"){
      that.setData({     
        ocr_content:content,
        modal_show:true
      })
    }else{
      that.setData({
        ocr_content:content,
        show_append_text:true
      })
    }
  },

  /**车牌识别*/
  platenumSuccess(info){
    let that = this 
    let result = info.detail    

    let number = result.number.text

    let content = number

    //新建识别任务
    if(that.data.type != "append"){
      that.setData({     
        ocr_content:content,
        modal_show:true
      })
    }else{
      that.setData({
        ocr_content:content,
        show_append_text:true
      })
    }
  },

  /**
   * 营业执照识别
   * @param {识别返回数据} info 
   */
  businessSuccess(info){
    let that = this

    let result = info.detail

    let address = "住所: "+result.address.text
    let business_scope = "经营范围: "+ result.business_scope.text
    let enterprise_name = "名称: "+ result.enterprise_name.text
    let legal_representative = "法定代表人: "+ result.legal_representative.text
    let reg_num = "统一社会信用代码: "+ result.reg_num.text
    let registered_capital = "注册资本: "+result.registered_capital.text
    let registered_date = "成立日期: "+ result.registered_date.text
    let serial = "编号: "+result.serial.text
    let title = result.serial.text
    let type_of_enterprise = "类型: "+ result.type_of_enterprise.text
    let valid_period = "营业期限: "+result.valid_period.text

    let content = title+"\n"+enterprise_name+"\n"+type_of_enterprise+"\n"+address+"\n"+legal_representative+"\n"+registered_capital+"\n"+registered_date+"\n"+valid_period+"\n"+business_scope

    //新建识别任务
    if(that.data.type != "append"){
      that.setData({     
        ocr_content:content,
        modal_show:true
      })
    }else{
      that.setData({
        ocr_content:content,
        show_append_text:true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  takePhoto() {
    let that = this
    let type = that.data.type

    wx.navigateTo({
      url: '../takePhoto/takePhoto?type='+type+'&filename='+that.data.filename
    })
  },

  getAlubm(){
    let that = this
    let type = that.data.type
    wx.navigateTo({
      url: '../Albums/Albums?type='+type+'&filename='+that.data.filename,
    })    
  },

      /**
   * 追加文字回调   * 
   */
  myAppendTxt(){
    let that = this
    let filename_suffix = that.data.filename
    let filename = filename_suffix.slice(0,filename_suffix.length - 5) //此处的文件名默认有.docx后缀，要不这个后缀去掉    

    let ocr_content = that.data.ocr_content
    let openid = wx.getStorageSync('openid')

    wx.request({
      method:"post",
      header: {
        'content-type': 'application/json' 
      },
      url:'https://admin.yzqysoft.com:8002/sendAppendContent',
      data: {
        fileName:filename,
        content:ocr_content,
        openid:openid
      },    
      success (res) {
        // 预览网络文档
        wx.downloadFile({
          //url: "http://121.37.180.71:8002/static/tmp_docx/"+openid+"/"+filename+".docx", // 文件的本身url
          url: "https://admin.yzqysoft.com:8002/static/tmp_docx/"+openid+"/"+filename+".docx", // 文件的本身url
          filePath: wx.env.USER_DATA_PATH + '/'+filename+".docx", // 本地自定义的文件名
          success: function (res) {
            let filePath = res.filePath; // 微信临时文件路径(这里要使用自定义的名字文件名,否则打开的文件名是乱码)
            wx.openDocument({
              filePath: filePath,
              showMenu: true,  // 是否显示右上角菜单按钮 默认为false(看自身需求，可要可不要。后期涉及到右上角分享功能)
              success: function () {
               wx.showToast({
                 title: '打开文件成功',
               })
              },
              fail: function() {
                wx.showToast({ title: '打开文件失败，请稍后重试' });
              }
            });           
          },
          fail: function() {
            wx.showToast({ title: '打开文件失败，请稍后重试' });           
          }
        });
      }
    })
  },

    /**
   * 关闭追加文字对话框
   */
  close_appendText_btn(){
    let that = this
    that.setData({
      show_append_text:false
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})